//
//  AiRoleViewModel.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/29.
//

import Foundation
class AiRoleViewModel : BaseViewModel {
    
    var delChatBg :String? = nil
    var delDiaryBg :String? = nil

    func upload(image: UIImage) {
        request {
            let bean = try await QiniuManager.shared.upload(image : image, key: "test",modeType: QiniuResult.self)
            return bean
        } suc: {
            showLog("成功\(String(describing: toJson($0)))")
        } fail: {
            showLog("错误信息\($0)")
        }
    }
    
    func doSave(bean: AiRoleBean,image: UIImage?,bgChat: UIImage?,bgDiary: UIImage?, isAdd: Bool) {
        request {
            try check(!(bean.name ?? "").isEmpty) {
                "名称不能为空"
            }
            try check(!(bean.name2 ?? "").isEmpty) {
                "对你的称呼不能为空"
            }
            try check(!(bean.describe ?? "").isEmpty) {
                "描述不能为空"
            }
            try check(!(bean.describe2 ?? "").isEmpty) {
                "关于我的描述不能为空"
            }
            try check(!(bean.sexName ?? "").isEmpty) {
                "请选择性别"
            }
            if image != nil {
                if !(bean.avatar ?? "").isEmpty {
                    //删除图片
                    try await retry(maxRetries: 10, retryDelayMillis: 200) {
                        _ = try await QiniuManager.shared.delete(url: bean.avatar ?? "", modeType: String.self)
                    }
                }
                //上传图片
                try await retry(maxRetries: 10, retryDelayMillis: 200) {
                    let time = getMillisecond(try await getNetTime())
                    let name = "ai/\(userManager.getUserId())_\(time)"
                    let result = try await QiniuManager.shared.upload(image : image!, key: name,modeType: QiniuResult.self)
                    bean.avatar = result.getUrl()
                }
            }
            try check(!(bean.avatar ?? "").isEmpty) {
                "头像不能为空"
            }
            if bgChat != nil {
                if !(bean.chatBg ?? "").isEmpty {
                    //删除图片
                    try await retry(maxRetries: 10, retryDelayMillis: 200) {
                        _ = try await QiniuManager.shared.delete(url: bean.chatBg ?? "", modeType: String.self)
                    }
                }
                //上传图片
                try await retry(maxRetries: 10, retryDelayMillis: 200) {
                    let time = getMillisecond(try await getNetTime())
                    let name = "ai/chatBg\(userManager.getUserId())_\(time)"
                    let result = try await QiniuManager.shared.upload(image : bgChat!, key: name,modeType: QiniuResult.self)
                    bean.chatBg = result.getUrl()
                }
            } else if self.delChatBg?.isEmpty == false {
                //删除图片
                try await retry(maxRetries: 10, retryDelayMillis: 200) {
                    _ = try await QiniuManager.shared.delete(url: self.delChatBg ?? "", modeType: String.self)
                }
            }
            
            if bgDiary != nil {
                if !(bean.diaryBg ?? "").isEmpty {
                    //删除图片
                    try await retry(maxRetries: 10, retryDelayMillis: 200) {
                        _ = try await QiniuManager.shared.delete(url: bean.diaryBg ?? "", modeType: String.self)
                    }
                }
                //上传图片
                try await retry(maxRetries: 10, retryDelayMillis: 200) {
                    let time = getMillisecond(try await getNetTime())
                    let name = "ai/diaryBg\(userManager.getUserId())_\(time)"
                    let result = try await QiniuManager.shared.upload(image : bgDiary!, key: name,modeType: QiniuResult.self)
                    bean.diaryBg = result.getUrl()
                }
            } else if self.delDiaryBg?.isEmpty == false {
                //删除图片
                try await retry(maxRetries: 10, retryDelayMillis: 200) {
                    _ = try await QiniuManager.shared.delete(url: self.delDiaryBg ?? "", modeType: String.self)
                }
            }
            try await retry(maxRetries: 10, retryDelayMillis: 200) {
                _ = try await BackupsManager.shared.getDelegateConfig()?.addAiRole(bean: bean, isAdd: isAdd)
            }
            return true
        } suc: { _ in
            showToast("保存成功")
            NotificationCenter.default.post(name: .updateAiRole, object: true)
            pathManager.back()
        } fail: {
            showToast($0.message)
        }
    }
    
}
